home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / tpc10.arc / TPC.DOC < prev    next >
Text File  |  1988-10-23  |  7KB  |  260 lines

  1.  
  2. TPC - Translate Pascal to C.
  3. (C) Copyright 1986 by Samuel H. Smith; All rights reserved.
  4.  
  5. Please refer all inquiries to:
  6.     S.H.Smith
  7.     5119 N 11 Ave 332
  8.     Phoenix, AZ 85013
  9.  
  10. You may copy and distribute this program freely, provided that:
  11.     1)   No fee is charged for such copying and distribution, and
  12.     2)   It is distributed ONLY in its original, unmodified state.
  13.  
  14. If you like this program, and find it of use, then your contribution 
  15. will be appreciated.  If you are using this product in a commercial 
  16. environment, then the contribution is not voluntary. 
  17.  
  18.  
  19. Usage:  TPC d:path\filename[.ext] [>outfile]
  20.  
  21.   d:   drive and path are optional
  22.   ext  defaults to .PAS
  23.  
  24. Example:
  25.         TPC program.pas >program.c
  26.  
  27.  
  28. This program will read a turbo pascal source file and convert it into 
  29. the corresponding C source code.   It does about 90% of the work 
  30. required to do the translation. 
  31.  
  32.  
  33. The following language constructs are translated:
  34. ------------------------------------------------
  35.  
  36.    Comments are translated from either {...} or (*...*) into /*...*/.
  37.  
  38.    Begin and End are translated into { and }.
  39.  
  40.    Const declarations are translated from
  41.       ID = VALUE
  42.    into
  43.       static ID = VALUE.
  44.  
  45.    Simple Var declarations are translated from
  46.       ID TYPE
  47.    into 
  48.       TYPE ID.
  49.  
  50.    The following simple types are translated
  51.       from  BOOLEAN    to   char,
  52.             INTEGER    to   int,
  53.             REAL       to   double.
  54.  
  55.    Record types are translated from 
  56.       ID = record MEMBER-LIST end
  57.    into
  58.       typedef struct { MEMBER-LIST } ID.
  59.  
  60.    Enumeration types are translated from 
  61.       ID = (...)
  62.    into
  63.       typedef enum {...} ID.
  64.  
  65.    Array types are translated from 
  66.       ID = array [RANGE] of TYPE
  67.    into
  68.       typedef TYPE ID[RANGE].
  69.  
  70.    Pointer types are translated from
  71.       ID = ^DEFINED-TYPE
  72.    into
  73.       DEFINED-TYPE *ID.
  74.  
  75.    String types are translated from
  76.       ID = string[N]
  77.    into 
  78.       typedef char ID[N].
  79.  
  80.    File types are translated from
  81.       ID = text[N]
  82.       ID = text
  83.    into
  84.       FILE *ID
  85.       int ID.
  86.  
  87.    For statements are translated from
  88.       for VAR := FIRST to LAST do STATEMENT
  89.       for VAR := FIRST downto LAST do statement
  90.    into
  91.       for (VAR = FIRST; VAR <= LAST; VAR++) STATEMENT
  92.       for (VAR = FIRST; VAR >= LAST; VAR--) STATEMENT
  93.  
  94.    While statements are translated from
  95.       while COND do STATEMENT
  96.    into
  97.       while (COND) statement.
  98.  
  99.    Repeat statements are translated from
  100.       repeat STATEMENTS until COND
  101.    into
  102.       do { STATEMENTS } while(!COND).
  103.  
  104.    If statements are translated from
  105.       if COND then STATEMENT else STATEMENT
  106.    into
  107.       if (COND) STATEMENT; else STATEMENT.
  108.  
  109.    Case statements are translated from
  110.       case VALUE of
  111.          V:    STATEMENT;
  112.          V,U:  STATEMENT;
  113.          else  STATEMENT
  114.       end
  115.    into
  116.       switch (VALUE) {
  117.          case V:  STATEMENT; break;
  118.          case V:  
  119.          case U:  STATEMENT; break;
  120.          default: STATEMENT; 
  121.       }.
  122.  
  123.    The IN operator is translated from
  124.       VAL in [A,B,C]
  125.     into 
  126.       inset(VAL, setof(A,B,C,-1)).
  127.  
  128.    The ParamCount and ParamStr functions are translated from
  129.       paramcount
  130.       paramstr(n)
  131.    into 
  132.       arcv
  133.       argv[n].
  134.  
  135.    Dummy parameter lists are added to function and procedure calls, 
  136.    where they are required in C but not in Pascal. 
  137.  
  138.    The following expression operators are translated
  139.       from  DIV  to   /,     MOD  to   %,
  140.             AND  to  &&,     OR   to  ||,
  141.             XOR  to   ~,     <>   to  !=,
  142.             NOT  to  ! ,     SHR  to  >>,
  143.             SHL  to  <<,     =    to  ==,
  144.             :=   to   =.
  145.  
  146.    The '^' symbol is translated
  147.       from  VAR^          to  *VAR,
  148.             VAR^.MEMBER   to  VAR->MEMBER.
  149.  
  150.    Exit statements are translated 
  151.       from  exit    to  return.
  152.  
  153.    The New operator is translated from
  154.       new(VAR)
  155.    into 
  156.       VAR = malloc(sizeof(*VAR)).
  157.  
  158.    The special value, nil, is translated into NULL.
  159.  
  160.    Procedure/function formal parameter lists are translated into a 
  161.       separate procedure declaration and parameter variable 
  162.       declarations, as required by C, e.g.
  163.    from
  164.       function NAME(V1: TYPE1; V2: TYPE2): TYPE3
  165.    into
  166.       TYPE3 NAME(V1,V2)
  167.       TYPE1 V1;
  168.       TYPE2 V2;
  169.  
  170.    Procedures are translated into functions with 'void' return types.
  171.  
  172.    The special character literal syntax, ^C or #nn, is translated into 
  173.    '\ooo', where ooo is the octal notation for the ascii code. 
  174.  
  175.    Hex constants $hhhh are translated into 0xhhhh.
  176.  
  177.    Write and WriteLn are translated from:
  178.       write(VAR,VAR:n,VAR:n:m)
  179.       writeln(FILE,VAR,VAR,VAR)
  180.    into
  181.       printf("%d%nd%n.md",VAR,VAR,VAR)
  182.       fprintf(FILE,"%d%d%d\n",VAR,VAR,VAR).
  183.  
  184.    Read and ReadLn are translated from:
  185.       read(VAR,VAR,VAR)
  186.       readln(FILE,VAR,VAR,VAR)
  187.    into
  188.       scanf("%d%nd%d",&VAR,&VAR,&VAR)
  189.       fscanf(FILE,"%d%d%d\n",&VAR,&VAR,&VAR).
  190.  
  191.    String assignments are translated from:
  192.       VAR := "string"
  193.       VAR := VAR1 + "string"
  194.    into
  195.       strcpy(VAR, "string")
  196.       strcpy(VAR, concat(VAR1, "string")).
  197.  
  198.    String comparisons are translated from:
  199.       VAR == "string"
  200.       VAR < "string"
  201.       "string" >= VAR
  202.    into
  203.       (strcmp(VAR,"string") == 0)
  204.       (strcmp(VAR,"string") < 0)
  205.       (strcmp("string",VAR) >= 0).
  206.  
  207.    Function value assignments are translated from:
  208.       FUN_NAME := expr
  209.    into
  210.       return expr.
  211.  
  212.    Compiler directives are translated
  213.       from  {$B+/-}   to  #pragma standard_io(ON/OFF),
  214.             {$C+/-}       #pragma control_c_check(ON/OFF),
  215.             {$D+/-}       #pragma device_check(ON/OFF),
  216.             {$Fn}         #pragma max_files(n),
  217.             {$Gn}         #pragma input_file_buffer(n),
  218.             {$I name}     #include "name",
  219.             {$I+/-}       #pragma io_error_check(ON/OFF),
  220.             {$K+/-}       #pragma stack_check(ON/OFF),
  221.             {$Pn}         #pragma output_file_buffer(n),
  222.             {$R+/-}       #pragma range_check(ON/OFF),
  223.             {$U+/-}       #pragma user_interrupt(ON/OFF),
  224.             {$V+/-}       #pragma param_type_check(ON/OFF).
  225.  
  226.  
  227.  
  228.  
  229. Some language features that are not translated:  
  230. -----------------------------------------------
  231.  
  232.    Nested procedures or function cannot be coded in C.
  233.  
  234.    VAR parameters must be manually recoded.
  235.  
  236.    File access procedures need to be coded (reset, assign, close, etc.).
  237.  
  238.    Ranges in the form VAL..VAL are not translated in case statements.
  239.  
  240.    Forward pointer type declarations are translated, but will not 
  241.    compile in C.  They must be manually recoded. 
  242.  
  243.    Variant record types should be translated into unions, but aren't.
  244.  
  245.    Bitwise AND and OR operators are always translated into the logical 
  246.    operators && and ||. 
  247.  
  248.    C operator precidence differs from that of Pascal, and the 
  249.    differences are not translated. 
  250.  
  251.    The With statement is not translated.
  252.  
  253.    The MEM[] and PORT[] arrays are not translated.  These should be 
  254.    turned int function calls. 
  255.  
  256.    Absolute variables are not (and probably cannot be) translated.
  257.  
  258.  
  259.  
  260.